﻿using System; 
using System.Collections.Generic;
using System.Text;
using XXX.Models;
using fuzhu;
using System.Data; 
using XXX.Models.XXXEntities;
using LH.U8API.Bus;
using ADODB;
using XXX.Common;
using System.Data.SqlClient;
using GLY_XieTong;
using System.ServiceModel;
using ServiceReference1;
using System.Xml.Linq;
using System.Linq;
using U8COWeb;
using log4net.Util;

namespace XXX.Bo
{
    public static class WMSService
    {

       public static string conStr = CanShu.conStr;
        //获取PDA上的版本号
        public static RetModelbool GetPdaVersion()
        {

            string sql = string.Format("select cvalue from AA_PARA WHERE CTYPE='PdaVersion' ");
            string cVersion = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, conStr));
            return DBService.ReOKbool(cVersion);
        }
      
       

        /// <summary>
        /// 盘点单扫描更新
        /// </summary>
        /// <returns></returns>
        public static RetModelbool ST_PanDianUpdate(PanDianVouch scanBarcodes)
        {
            try
            {
                string conStr = Common.AppSettings.GetAppSeting(scanBarcodes.cOrg);
                if (string.IsNullOrEmpty(conStr))
                {
                    throw new Exception("未配置账套数据库");
                }
                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                if (scanBarcodes.barcodelist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                RetModel retModel = new RetModel();
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                
                try
                {
                    //扫码记录的保存到原始表，wms系统
                    for (int i = 0; i < scanBarcodes.barcodelist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.barcodelist[i].cBarcode), 
                         new SqlParameter("@qty",scanBarcodes.barcodelist[i].iQuantity),
                            new SqlParameter("@invcode",scanBarcodes.barcodelist[i].cInvCode),
                               new SqlParameter("@poscode",DbHelper.ToDbValue(scanBarcodes.barcodelist[i].cPosCode)),
                                  new SqlParameter("@batch",DbHelper.ToDbValue(scanBarcodes.barcodelist[i].cBatch)),
                         new SqlParameter("@ctype","盘点"),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",DateTime.Now.ToString("yyyy-MM-dd")),
                          new SqlParameter("@userid",scanBarcodes.cUserId), 
                           new SqlParameter("@sourcecode",scanBarcodes.cCVCode)
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,invcode,poscode,batch,
qty,ctype,cmaker,ddate,userid, sourcecode) values(@parentid,@barcodeno, @invcode,@poscode,@batch,
@qty,@ctype,@cmaker,@ddate,@userid, @sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                   

                //更新U8
                for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                {
                    //有autoid的，根据autoid 更新
                    int iAutoid = scanBarcodes.fenpeilist[i].autoID;
                    decimal scanqty  = scanBarcodes.fenpeilist[i].scanqty;
                    if (iAutoid > 0 && scanqty > 0)
                    {
                        sql = string.Format("UPDATE CheckVouchs SET iCVCQuantity = iCVCQuantity+{1} WHERE autoID = '{0}' ", iAutoid, scanqty);
                        DbHelper2.ExecuteNonQuery(sql, conStr);
                    }
                    else if(iAutoid == 0 && scanqty > 0)
                    {
                        //取最大的rowno
                        sql = string.Format(@"select max(irowno) from checkvouchs where ccvcode='{0}'", scanBarcodes.cCVCode);
                        int iMaxRow =DbHelper.GetDbInt(DbHelper2.ExecuteScalar(sql, conStr))+1;
                        //获取autoid
                        string cAcc = DbHelper.GetDbString(DbHelper2.ExecuteScalar("SELECT  DBO.fn_fd_getAccId()", conStr)) ;
                        int iRdid = 0;
                        int iRdsid = 0;

                        //获取插入行的autoid
                        SqlParameter[] param = new SqlParameter[]{
                                         new SqlParameter("@RemoteId","00"),
                                         new SqlParameter("@cAcc_Id",cAcc),
                                         new SqlParameter("@cVouchType","ch"),
                                         new SqlParameter("@iAmount",1),
                                         new SqlParameter("@iFatherId",iRdid),
                                         new SqlParameter("@iChildId",iRdsid)

          };
                        param[4].Direction = ParameterDirection.Output;
                        param[5].Direction = ParameterDirection.Output;
                        DbHelper2.ExecuteSqlWithTrans("sp_getid", param, CommandType.StoredProcedure, tran);
                        iRdid = DbHelper.GetDbInt(param[4].Value);
                        iRdsid = DbHelper.GetDbInt(param[5].Value)-1; 


                        //有保质期的，计算保质期。参照getinfo的计算方式
                        SqlParameter[] param2 = new SqlParameter[]{
                        new SqlParameter("@ccvcode",scanBarcodes.cCVCode),
                         new SqlParameter("@cinvcode",scanBarcodes.fenpeilist[i].cInvCode),
                         new SqlParameter("@icvcnum",0),
                            new SqlParameter("@icvcquantity",scanBarcodes.fenpeilist[i].iCVCQuantity),
                               new SqlParameter("@ccvbatch",scanBarcodes.fenpeilist[i].cCVBatch),
                                  new SqlParameter("@cposition",scanBarcodes.fenpeilist[i].cPosCode),
                         new SqlParameter("@id",scanBarcodes.id),
                         new SqlParameter("@cassunit",DbHelper.ToDbValue(null)),
                         new SqlParameter("@imassdate",DbHelper.ToDbValue(null)),
                          new SqlParameter("@cmassunit",DbHelper.ToDbValue(null)),
                           new SqlParameter("@dmadedate",DbHelper.ToDbValue(null)) ,
                           new SqlParameter("@irowno",iMaxRow),
                            new SqlParameter("@dDisDate",DbHelper.ToDbValue(null))
                        };
                        sql = string.Format(@"INSERT INTO checkvouchs(autoid,ccvcode,cinvcode,icvnum,icvquantity,icvcnum,icvcquantity,ccvbatch,cposition,id,cassunit,imassdate,dmadedate,cmassunit,dDisDate,irowno)
VALUES(@autoid,@ccvcode,@cinvcode,0, 0,@icvcnum, @icvcquantity, @ccvbatch, @cposition, @id, @cassunit, @imassdate, @dmadedate, @cmassunit,@dDisDate, @irowno) ", iAutoid, scanqty);
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);
                    }
                    //没有的,插入新行

                }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }
                return DBService.ReOKbool();
            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }

        /// <summary>
        /// 盘点单删除
        /// </summary>
        /// <returns></returns>
        public static RetModelbool ST_PanDianDel(string corg,string ccode)
        {
            try
            {
                string conStr = Common.AppSettings.GetAppSeting(corg);
                if (string.IsNullOrEmpty(conStr))
                {
                    throw new Exception("未配置账套数据库");
                }

                string sql = string.Format(@"UPDATE CheckVouchs SET iCVCQuantity = 0 FROM CheckVouchs a,dbo.CheckVouch b
 WHERE a.cCVCode = '{0}' AND a.cCVCode = b.cCVCode
AND ISNULL(b.cAccounter, '') = '' ",ccode);
                DbHelper2.ExecuteNonQuery(sql, conStr);
                //删除扫描的清单
                 

                return DBService.ReOKbool();
            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }

        /// <summary>
        /// 根据到货写入库
        /// </summary>
        /// <param name="id">到货单id</param>
        /// <param name="parentid">条码</param>
        /// <param name="cwhcode">入库仓库</param>
        /// <returns></returns>
//        public static string PuArrToRd01ById(int id, string cParentid, string cwhcode)
//        {

//            //插入U8
//            try
//            {
//                string sql = string.Format(@"SELECT a.cCode,a.cVenCode,a.cDepCode,a.cPersonCode,
//a.cexch_name, cmaker,a.iExchRate,b.cInvCode,cbatch,b.iTaxRate,b.iOriTaxCost,b.iOriCost,
//b.iQuantity,b.iNum,b.cUnitID,b.Autoid,B.cBatch,B.dPDate FROM dbo.PU_ArrivalVouch a, dbo.PU_ArrivalVouchs b
//WHERE a.id = b.id and a.id = '{0}' and b.bgsp=0 ", id);
//                DataTable dtsum = DbHelper2.ExecuteTable(sql, CanShu.conStr);

//                    LH.Model.ST.Rdrecord01.Head rdHead = new LH.Model.ST.Rdrecord01.Head();

//                    rdHead.cvouchtype = "01";
//                    rdHead.crdcode = GetRdcode("","采购入库");
//                    //rdHead.cbustype = "普通采购";
//                    rdHead.csource = "采购到货单";
//                    rdHead.cwhcode = cwhcode;
//                    rdHead.ddate = DateTime.Now.ToString("yyyy-MM-dd");
//                    rdHead.cmaker = DbHelper.GetDbString(dtsum.Rows[0]["cmaker"]);
//                    rdHead.idiscounttaxtype = "0";
//                    rdHead.bredvouch = "0";
//                    rdHead.brdflag = "1";
//                    rdHead.iPrintCount = "0";
//                    rdHead.ccode = DbHelper.GetDbString(dtsum.Rows[0]["cCode"]); ;
//                    rdHead.cdefine10 = cParentid;   //入库单号写自定义项1
//                                                    //部门、人员，供应商等，根据表体的订单id取值


//                    List<LH.Model.ST.Rdrecord01.Body> bodies = new List<LH.Model.ST.Rdrecord01.Body>();

//                    for (int i = 0; i < dtsum.Rows.Count; i++)
//                    {
//                        LH.Model.ST.Rdrecord01.Body body = new LH.Model.ST.Rdrecord01.Body();
//                        body.cinvcode = DbHelper.GetDbString(dtsum.Rows[i]["cInvCode"]);
//                        body.cbatch = DbHelper.GetDbString(dtsum.Rows[i]["cBatch"]);
//                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["dPDate"])))
//                        {
//                            body.dmadedate = DbHelper.GetDbDate(dtsum.Rows[i]["dPDate"]).ToString("yyyy-MM-dd");
//                        }
//                        //body.cposition = model.materialList[i].cposcode;
//                        body.iarrsid = DbHelper.GetDbString(dtsum.Rows[i]["Autoid"]);
//                        //body.dmadedate = model.materialList[i].manufactureDate;
//                        body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["iQuantity"]));
//                        //body.inum = model.materialList[i].count;
//                        body.editprop = "A";
//                        body.irowno = (i + 1).ToString();


//                        bodies.Add(body);
//                    }



//                    LH.Model.ST.Rdrecord01 entity = new LH.Model.ST.Rdrecord01(rdHead, bodies);
//                    string cRe = GetInfo.GetST01ByArrInfo(entity, CanShu.conStr);
//                    if (cRe != "ok")
//                    {
//                        throw new Exception(cRe);
//                    }
//                //新增
//                string cerr, cid, ccode;
//                if (!Rdrecord01DALCO.AddRdrecord01(entity, out cerr, out cid, out ccode))
//                {
//                    throw new Exception(cerr);
//                }



                 


//                //更新条码的状态
//                BarcodeManager barcodeManager = new BarcodeManager();
//                barcodeManager.ChangeBarcodestate(cParentid, "在库");
//                return "ok";
//            }
//            catch (Exception ex)
//            {
//                DelVouch(cParentid);
//                return ex.Message;
//            }



//        }

        
        

            /// <summary>
            /// 根据到货单生成入库单
            /// </summary>
            /// <param name="iid"></param>
            /// <returns></returns>
            public static RetModelbool PU_ArrToRd01(PuArrivalBarcode scanBarcodes)
        {
          
            
            try
            {
                string conStr = Common.AppSettings.GetAppSeting(scanBarcodes.cOrg);
                if (string.IsNullOrEmpty(conStr))
                {
                    throw new Exception("未配置账套数据库");
                }

                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表WMS_RDRECORDS_ORI.欧瑞京，条码只有存货编码，所以在保存原始数据的时候，把批次，货位一起保存
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                          new SqlParameter("@invcode",scanBarcodes.fenpeilist[i].cInvCode),
                         new SqlParameter("@batch",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cBatch)),
                          new SqlParameter("@poscode",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cPosCode)),
                          new SqlParameter("@whcode",DbHelper.ToDbValue(scanBarcodes.cWhcode)),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                         new SqlParameter("@userid",scanBarcodes.cUserId),
                         new SqlParameter("@vencode",DbHelper.ToDbValue(scanBarcodes.cVenCode)),
                         new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                         new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,invcode,batch,poscode,whcode,sourcerowid,
qty,ctype,cmaker,ddate,userid,vencode,depcode,personcode,ptcode,sourcecode) values(@parentid,@barcodeno,@invcode,@batch,@poscode,@whcode,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@vencode,@depcode,@personcode,@ptcode,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);
                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                RetModel retModel = new RetModel();
                string cRdcode = GetRdcode(conStr,scanBarcodes.cType);
                string cCode = GetNewcode(conStr,"rdrecord01");

                int iRdflag = 1;
                int ibs = 1;  //数量计算 *1或*-1
                if (scanBarcodes.cType == "采购退货")
                {
                    iRdflag = 0;
                    ibs = -1;
                }
                //取表体的信息，汇总. makedate有效期没写

                DataTable dtsum = OMS_GetBarcodeDetail(conStr, cParentid);

                //查询是委外到货还是普通采购
                sql = string.Format(@"select cBusType from PU_ArrivalVouch where ccode='{0}'", scanBarcodes.cCode);
                string cBustype = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, conStr));


                //插入U8
                try
                {
                    
                        LH.Model.ST.Rdrecord01.Head rdHead = new LH.Model.ST.Rdrecord01.Head();

                        rdHead.cvouchtype = "01";
                        rdHead.crdcode = cRdcode;
                        rdHead.cbustype = cBustype;
                        if (cBustype == "委外加工")
                        {
                            rdHead.csource = "委外到货单";
                        }
                        else
                        {
                            rdHead.csource = "采购到货单";
                        }
                       
                   
                        rdHead.cwhcode = scanBarcodes.cWhcode;
                        rdHead.ddate = scanBarcodes.dDate.ToString("yyyy-MM-dd");
                       rdHead.ccode = cCode;
                    rdHead.cmaker = scanBarcodes.cUserName;
                    //rdHead.cdepcode = "0401";
                    //rdHead.cvencode = "111";
                    //rdHead.chandler = model.verifier;
                    //rdHead.vt_id = "27";
                    //rdHead.itaxrate = "13.00";
                    //rdHead.iexchrate = "1";
                    //rdHead.cexch_name = "人民币";
                        rdHead.idiscounttaxtype = "0";
                        rdHead.bredvouch = "0";
                    
                    rdHead.brdflag = "1";
                        //rdHead.cpersoncode = "00043";
                        //rdHead.cordercode = "0000000052";
                        //rdHead.ipurorderid = "1000000054";
                        rdHead.iPrintCount = "0";
                    rdHead.cdefine14 = cParentid;


                    List<LH.Model.ST.Rdrecord01.Body> bodies = new List<LH.Model.ST.Rdrecord01.Body>();
                    //根据扫描数量生成表体，不是根据分配的明细
                  
                        for (int i = 0; i < dtsum.Rows.Count; i++)
                        {
                        
                            LH.Model.ST.Rdrecord01.Body body = new LH.Model.ST.Rdrecord01.Body();
                            body.cinvcode = DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]);
                            body.cbatch = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                             body.cposition = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]);
                           body.iarrsid = DbHelper.GetDbString(dtsum.Rows[i]["SOURCEROWID"]);
                            //body.dmadedate = model.materialList[i].manufactureDate;
                            body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                            //body.inum = model.materialList[i].count;
                            body.editprop = "A";
                            //if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["MAKEDATE"])))
                            //{
                            //    body.dmadedate = DbHelper.GetDbDate(dtsum.Rows[i]["MAKEDATE"]).ToString("yyyy-MM-dd");
                            //}
                            
                            body.irowno = (i+1).ToString();
                            bodies.Add(body);

                        }
                            
                        LH.Model.ST.Rdrecord01 entity = new LH.Model.ST.Rdrecord01(rdHead, bodies);
                    string cRe = GetInfo.GetST01ByArrInfo(entity, conStr);
                    if (cRe != "ok")
                    {
                        throw new Exception(cRe);
                    }
                    string cerr, cid, ccode;
                    string xmlconfig = XmlOperation.GetConfigXML(scanBarcodes.cOrg, scanBarcodes.cUserName, scanBarcodes.cPwd).ToString();
                    if (!Rdrecord01DALCO.AddVerifyRdrecord01(entity, xmlconfig, out cerr, out cid, out ccode))
                    {
                        throw new Exception(cerr);
                    }
                    ////审核
                    //if (!Rdrecord01DALCO.AddRdrecord01(entity, xmlconfig, out cerr, out cid, out ccode))
                    //{
                    //    throw new Exception(cerr);
                    //}


                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    DelVouch(conStr,cParentid);
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }


     // <summary>
        /// 插入扫描的原始条码
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool PuArrQCToRd01(PuArrivalBarcode scanBarcodes)
        {

            try
            {
                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                RetModel retModel = new RetModel();
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                          new SqlParameter("@userid",scanBarcodes.cUserId),
                           new SqlParameter("@vencode",DbHelper.ToDbValue(scanBarcodes.cVenCode)),
                            new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                             new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                              new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                               new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,sourcerowid,
qty,ctype,cmaker,ddate,userid,vencode,depcode,personcode,ptcode,sourcecode) values(@parentid,@barcodeno,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@vencode,@depcode,@personcode,@ptcode,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }
                   
                //生成新的单据号
                string cVouchCode = GetNewcode(conStr, "WMS_RDRECORD");
                string cRdcode = GetRdcode("",scanBarcodes.cType);

                int iRdflag = 1;
                int ibs = 1;  //数量计算 *1或*-1
                if (scanBarcodes.cType == "采购退货")
                {
                    iRdflag = 0;
                    ibs = -1;
                }
                //取表体的信息，汇总. makedate有效期没写

                DataTable dtsum = GetBarcodeDetail(cParentid);

                tran = DbHelper2.BeginTrans(conStr);
                try
                {
                    SqlParameter[] param2 = new SqlParameter[]{
                        new SqlParameter("@ID",cParentid),
                         new SqlParameter("@CCODE",cVouchCode),
                         new SqlParameter("@DDATE",scanBarcodes.dDate),
                         new SqlParameter("@RDFLAG",iRdflag),
                          new SqlParameter("@RDCODE",DbHelper.ToDbValue(cRdcode)),
                         new SqlParameter("@WHCODE",scanBarcodes.cWhcode),
                         new SqlParameter("@DEPCODE",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         new SqlParameter("@PERSONCODE",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                          new SqlParameter("@VENCODE",DbHelper.ToDbValue(scanBarcodes.cVenCode)),
                          new SqlParameter("@PTCODE",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                          new SqlParameter("@SOURCECODE",DbHelper.ToDbValue(scanBarcodes.cCode)),
                          new SqlParameter("@CREATEUSERID",scanBarcodes.cUserId),
                          new SqlParameter("@CREATEBY",scanBarcodes.cUserName),
                           new SqlParameter("@vouchtype","采购入库")
          };

                    sql = string.Format(@"INSERT INTO [dbo].[WMS_RDRECORD]
           ([ID]
           ,[CCODE]
           ,[DDATE]
           ,[RDCODE]
           ,[RDFLAG]
           ,[WHCODE]
           ,[DEPCODE]
           ,[PERSONCODE]
           ,[VENCODE]
           ,[PTCODE]
           ,[SOURCECODE]
           ,[DELETEMARK]
           ,[CREATEON]
           ,[CREATEUSERID]
           ,[CREATEBY],vouchtype) values(@ID
           ,@CCODE
           ,@DDATE
           ,@RDCODE
           ,@RDFLAG
           ,@WHCODE
           ,@DEPCODE
           ,@PERSONCODE
           ,@VENCODE
           ,@PTCODE
           ,@SOURCECODE
           ,0
           ,GETDATE()
           ,@CREATEUSERID
           ,@CREATEBY,@vouchtype)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param2, tran);



                    for (int i = 0; i < dtsum.Rows.Count; i++)
                    {
                        SqlParameter[] param3 = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@INVCODE",dtsum.Rows[i]["INVCODE"]),
                         new SqlParameter("@UNIT",dtsum.Rows[i]["UNITNAME"]),
                         new SqlParameter("@ROWNO",(i+1).ToString()),
                         new SqlParameter("@BATCH",dtsum.Rows[i]["BATCH"]),
                         new SqlParameter("@QUANTITY",DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"])*ibs),
                         new SqlParameter("@MAKEDATE",DbHelper.ToDbValue(dtsum.Rows[i]["MAKEDATE"])),
                          new SqlParameter("@DELETEMARK","0"),
                           new SqlParameter("@SOURCEROWID",DbHelper.ToDbValue(dtsum.Rows[i]["SOURCEROWID"]))

          };
                        sql = @"insert into WMS_RDRECORDS([ID]
           ,[PARENTID]
           ,[ROWNO]
           ,[INVCODE]
           ,[UNIT]
           ,[BATCH]
           ,[QUANTITY]
           ,[MAKEDATE]
           ,[DELETEMARK]
           ,[SOURCEROWID]) values(newid(),@PARENTID
           ,@ROWNO
           ,@INVCODE
           ,@UNIT
           ,@BATCH
           ,@QUANTITY
           ,@MAKEDATE
           ,@DELETEMARK
           ,@SOURCEROWID)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param3, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                //插入U8
                try
                {
                    string cRe = DBService.GetUserLogin("1");
                    if (cRe != "ok")
                    {
                        throw new Exception(cRe);
                    }

                     
                        LH.Model.ST.Rdrecord01.Head rdHead = new LH.Model.ST.Rdrecord01.Head();

                        rdHead.cvouchtype = "01";
                        rdHead.crdcode = cRdcode;
                        //rdHead.cbustype = "普通采购";
                        rdHead.csource = "来料检验单";
                        rdHead.cwhcode = scanBarcodes.cWhcode;
                        rdHead.ddate = scanBarcodes.dDate.ToString("yyyy-MM-dd");
                        //rdHead.cdepcode = "0401";
                        //rdHead.cvencode = "111";
                        rdHead.cmaker = scanBarcodes.cUserName;
                        //rdHead.chandler = model.verifier;
                        //rdHead.vt_id = "27";
                        //rdHead.itaxrate = "13.00";
                        //rdHead.iexchrate = "1";
                        //rdHead.cexch_name = "人民币";
                        rdHead.idiscounttaxtype = "0";
                        rdHead.bredvouch = "0";
                        //rdHead.cdefine1 = "顾潇";
                        rdHead.brdflag = "1";
                        //rdHead.cpersoncode = "00043";
                        //rdHead.cordercode = "0000000052";
                        //rdHead.ipurorderid = "1000000054";
                        rdHead.iPrintCount = "0";
                        rdHead.ccode = "WMS" + cVouchCode;
                        rdHead.cdefine10 = cParentid;   //入库单号写自定义项1
                                                       //部门、人员，供应商等，根据表体的订单id取值


                        List<LH.Model.ST.Rdrecord01.Body> bodies = new List<LH.Model.ST.Rdrecord01.Body>();

                        for (int i = 0; i < dtsum.Rows.Count; i++)
                        {
                            LH.Model.ST.Rdrecord01.Body body = new LH.Model.ST.Rdrecord01.Body();
                            body.cinvcode = DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]);
                            body.cbatch = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                            //body.cposition = model.materialList[i].cposcode;
                            body.icheckidbaks = DbHelper.GetDbString(dtsum.Rows[i]["SOURCEROWID"]);
                            //body.dmadedate = model.materialList[i].manufactureDate;
                            body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                            //body.inum = model.materialList[i].count;
                            body.editprop = "A";
                            if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["MAKEDATE"])))
                            {
                                body.dmadedate = DbHelper.GetDbDate(dtsum.Rows[i]["MAKEDATE"]).ToString("yyyy-MM-dd");
                            }
                            body.irowno = (i + 1).ToString();


                            bodies.Add(body);
                        }



                        LH.Model.ST.Rdrecord01 entity = new LH.Model.ST.Rdrecord01(rdHead, bodies);
                        cRe = GetInfo.GetST01ByCheckInfo(entity, CanShu.conStr);
                        if (cRe != "ok")
                        {
                            throw new Exception(cRe);
                        }
                        //新增
                        string cerr, cid, ccode;
                        if (!Rdrecord01DALCO.AddRdrecord01(entity, out cerr, out cid, out ccode))
                        {
                            throw new Exception(cerr);
                        }


                        //MessageBox.Show(id);
                        //审核
                        //LH.U8API.Bus.Pu_ArrivalVouchDAL.ConfirmArrivalVouch(id, con, u8login, true, true);
                        //MessageBox.Show("审核成功");
                        

                    //更细条码的状态
                    BarcodeManager barcode = new BarcodeManager();
                    barcode.ChangeBarcodestate(cParentid, "在库");
                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    DelVouch(conStr, cParentid);
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }


        
        /// <summary>
        /// 审核调拨单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool ST_VerifyTransVouch(PuArrivalBarcode scanBarcodes)
        {
           try
            {
                string conStr = Common.AppSettings.GetAppSeting(scanBarcodes.cOrg);
                if (string.IsNullOrEmpty(conStr))
                {
                    throw new Exception("未配置账套数据库");
                }

                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表WMS_RDRECORDS_ORI.欧瑞京，条码只有存货编码，所以在保存原始数据的时候，把批次，货位一起保存
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                          new SqlParameter("@invcode",scanBarcodes.fenpeilist[i].cInvCode),
                         new SqlParameter("@batch",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cBatch)),
                          new SqlParameter("@poscode",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cPosCode)),
                            new SqlParameter("@poscode2",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cPosCode2)),
                          new SqlParameter("@whcode",DbHelper.ToDbValue(scanBarcodes.cWhcode)),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                         new SqlParameter("@userid",scanBarcodes.cUserId),
                         new SqlParameter("@vencode",DbHelper.ToDbValue(scanBarcodes.cVenCode)),
                         new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                         new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,invcode,batch,poscode,poscode2,whcode,sourcerowid,
qty,ctype,cmaker,ddate,userid,vencode,depcode,personcode,ptcode,sourcecode) values(@parentid,@barcodeno,@invcode,@batch,@poscode,@poscode2,@whcode,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@vencode,@depcode,@personcode,@ptcode,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }


                RetModel retModel = new RetModel();

                String getMainSql = String.Format("select 'M' as editprop,* from TransM with(nolock) where ctvcode ='{0}'", scanBarcodes.cCode);
                DataTable mainDt = DbHelper2.ExecuteTable(getMainSql, conStr);
                LH.Model.ST.TransVouch.Head sHead = LH.U8API.Tool.U8APIHelper.EntityList<LH.Model.ST.TransVouch.Head>(mainDt)[0];


                //取表体的信息，汇总. makedate有效期没写。  更新货位信息

                DataTable dtsum = OMS_GetBarcodeDetail(conStr, cParentid);
                //更改货位信息，读取当前的货位信息
                String getDetailSql = String.Format("select a.* ,'' as editprop  from TransD  a with(nolock) , transvouch b with(nolock)   WHERE a.id = b.id and  b.ctvcode='{0}'", scanBarcodes.cCode);
                DataTable detailDt = DbHelper2.ExecuteTable(getDetailSql, conStr);
                List<LH.Model.ST.TransVouch.Body> sBody = LH.U8API.Tool.U8APIHelper.EntityList<LH.Model.ST.TransVouch.Body>(detailDt);
                int bodycnt = sBody.Count;  //读取body数量，避免中间增加改变body值

                for (int j = 0; j < bodycnt; j++)
                {
                    string iautoid = sBody[j].autoID;
                    int icnt = 0;//匹配了几个一样的
                                 //循环,如果autoid一样的,更改数量 和货位

                    for (int i = 0; i < dtsum.Rows.Count; i++)
                    {
                        string iautoid2 =DbHelper.GetDbString( dtsum.Rows[i]["SOURCEROWID"]);
                        int ifirst = 0; //第一个匹配的
                        if (iautoid == iautoid2)
                        {

                            if (sBody[j].cInvCode != DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]) )
                            {
                                throw new Exception(string.Format("行{0}和出库单存货编码不对应", iautoid2));
                            }
                            //if (!string.IsNullOrEmpty(sBody[j].cbatch) && string.IsNullOrEmpty(model.materialList[i].batchNum) && sBody[j].cbatch != model.materialList[i].batchNum)
                            //{
                            //    throw new Exception(string.Format("行{0}出库批次和出库单不对应", iautoid2));
                            //}

                            if (icnt == 0)
                            {
                                sBody[j].editprop = "M"; 
                                sBody[j].iTVQuantity = DbHelper.GetDbString(dtsum.Rows[i]["QTY"]);
                                sBody[j].coutposcode = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]);
                                sBody[j].cinposcode = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE2"]);
                                ifirst = j;
                            }
                            else
                            {
                                //插入新增行

                                LH.Model.ST.TransVouch.Body body = new LH.Model.ST.TransVouch.Body();
                                //body = sBody[ifirst];
                                body.editprop = "A";
                                //body.autoid = "0";
                                body.irowno = (sBody.Count + 1).ToString();
                                body.cInvCode = sBody[ifirst].cInvCode;
                                body.cTVBatch = sBody[ifirst].cTVBatch; 
                                body.cmocode = sBody[ifirst].cmocode;
                                body.imoseq = sBody[ifirst].imoseq;
 
                                body.invcode = sBody[ifirst].invcode;
                                body.cMassUnit = sBody[ifirst].cMassUnit;
                                body.iMassDate = sBody[ifirst].iMassDate;
                                body.dMadeDate = sBody[ifirst].dMadeDate;
                                body.dDisDate = sBody[ifirst].dDisDate;
                                body.cExpirationdate = sBody[ifirst].cExpirationdate;
                                body.iExpiratDateCalcu = sBody[ifirst].iExpiratDateCalcu;

                                body.iTVQuantity = DbHelper.GetDbString(dtsum.Rows[i]["QTY"]);
                                body.coutposcode = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]);
                                body.cinposcode = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE2"]);
                                sBody.Add(body);
                            }
                            icnt++;

                        }
                    }
                    if (icnt == 0)
                    {
                        throw new Exception(string.Format("行{0}没有出库确认信息", iautoid));
                    }


                }

                //审核U8
                try
                {
                    //string cRe = DBService.GetUserLogin("1");
                    //if (cRe != "ok")
                    //{
                    //    throw new Exception(cRe);
                    //}
                    LH.Model.ST.TransVouch sEntity = new LH.Model.ST.TransVouch(sHead, sBody);


                    sql = string.Format("select id,cverifyperson from transvouch where ctvcode='{0}'", scanBarcodes.cCode);
                    DataTable dt = DbHelper2.ExecuteTable(sql, conStr);
                    if (dt.Rows.Count > 0)
                    {
                        string iId = DbHelper.GetDbString(dt.Rows[0]["id"]);
                        string cHandler = DbHelper.GetDbString(dt.Rows[0]["cverifyperson"]);
                        if (!string.IsNullOrEmpty(cHandler))
                        {
                            throw new Exception("单据已审核");
                        }


                        string xmlconfig = XmlOperation.GetConfigXML(scanBarcodes.cOrg, scanBarcodes.cUserName, scanBarcodes.cPwd).ToString();
                        string err, cid, ccode;
                        //先修改
                        if (!TransVouchDALCO.ModifyTransvouch(sEntity, xmlconfig, out err, out cid, out ccode))
                        {
                            throw new Exception(err);
                        }
                        //再审核
                        if (!TransVouchDALCO.VerifyTransvouch(iId, xmlconfig, "verify", out err))
                        {
                            throw new Exception(err);
                        }
                        else
                        {
                            //scanBarcodes.cUserName
                            sql = string.Format("update transvouch set  iswfcontrolled = 0 WHERE id='{0}'  ", iId, scanBarcodes );
                            DbHelper2.ExecuteNonQuery(sql, conStr);

                        }
                      
                        //审核其他出入库单

                        string cerr;
                        sql = string.Format("select id from rdrecord08 where cbustype='调拨入库' and  cbuscode = '{0}'", scanBarcodes.cCode);
                            string iid8 = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, conStr));
                            if (!Rdrecord08DALCO.VerifyRdrecord08(iid8, xmlconfig, "verify", out cerr))
                            { 
                                  throw new Exception(cerr);
                            }
                            else
                            {
                                sql = string.Format("update rdrecord08 set   chandler='陆婷婷' WHERE id='{0}' and isnull(chandler,'')<>''", iid8);
                                DbHelper2.ExecuteNonQuery(sql, conStr);
                            }
                            sql = string.Format("select id from rdrecord09 where cbustype='调拨出库' and  cbuscode = '{0}'", scanBarcodes.cCode);
                            string iid9 = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, conStr));
                        if (!Rdrecord09DALCO.VerifyRdrecord09(iid9, xmlconfig, "verify", out cerr))
                        {
                            throw new Exception(cerr);
                        }
                        else
                        {

                            sql = string.Format("update rdrecord09 set chandler='陆婷婷',iswfcontrolled = 0 WHERE id='{0}' and isnull(chandler,'')<>''", iid9);
                            DbHelper2.ExecuteNonQuery(sql, conStr);
                        }
                        //回写表头自定义项14
                        sql = string.Format("update transvouch set cdefine14='{1}' from transvouch where ctvcode='{0}'", scanBarcodes.cCode,cParentid);
                        DbHelper2.ExecuteNonQuery(sql, conStr);
                    }


                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }

        /// <summary>
        /// 审核材料出库单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool ST_VerifyMaterialOut(PuArrivalBarcode scanBarcodes)
        {

            try
            {
                string conStr = Common.AppSettings.GetAppSeting(scanBarcodes.cOrg);
                if (string.IsNullOrEmpty(conStr))
                {
                    throw new Exception("未配置账套数据库");
                }

                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表WMS_RDRECORDS_ORI.欧瑞京，条码只有存货编码，所以在保存原始数据的时候，把批次，货位一起保存
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                          new SqlParameter("@invcode",scanBarcodes.fenpeilist[i].cInvCode),
                         new SqlParameter("@batch",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cBatch)),
                          new SqlParameter("@poscode",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cPosCode)),
                          new SqlParameter("@whcode",DbHelper.ToDbValue(scanBarcodes.cWhcode)),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                         new SqlParameter("@userid",scanBarcodes.cUserId),
                         new SqlParameter("@vencode",DbHelper.ToDbValue(scanBarcodes.cVenCode)),
                         new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                         new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,invcode,batch,poscode,whcode,sourcerowid,
qty,ctype,cmaker,ddate,userid,vencode,depcode,personcode,ptcode,sourcecode) values(@parentid,@barcodeno,@invcode,@batch,@poscode,@whcode,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@vencode,@depcode,@personcode,@ptcode,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                int ibs = 1;  //数量计算 *1或*-1
                if (scanBarcodes.cType == "材料退库")
                {
                    
                    ibs = -1;
                }
                RetModel retModel = new RetModel();

                String getMainSql = String.Format("select 'M' as editprop,* from RecordOutQ with(nolock) where ccode ='{0}'", scanBarcodes.cCode);
                DataTable mainDt = DbHelper2.ExecuteTable(getMainSql, conStr);
                LH.Model.ST.Rdrecord11.Head sHead = LH.U8API.Tool.U8APIHelper.EntityList<LH.Model.ST.Rdrecord11.Head>(mainDt)[0];


                //取表体的信息，汇总. makedate有效期没写。  更新货位信息

                DataTable dtsum = OMS_GetBarcodeDetail(conStr, cParentid);
                //更改货位信息，读取当前的货位信息
                String getDetailSql = String.Format("select a.*,CONVERT(NCHAR,CONVERT(MONEY,b.rowufts),2) as rowufts from RecordOutSQ a with(nolock),rdrecords11 b with(nolock) WHERE a.autoid=b.AutoID  and a.id={0}", sHead.id);
                DataTable detailDt = DbHelper2.ExecuteTable(getDetailSql, conStr);
                List<LH.Model.ST.Rdrecord11.Body> sBody = LH.U8API.Tool.U8APIHelper.EntityList<LH.Model.ST.Rdrecord11.Body>(detailDt);
                int bodycnt = sBody.Count;  //读取body数量，避免中间增加改变body值

                for (int j = 0; j < bodycnt; j++)
                {
                    string iautoid = sBody[j].autoid;
                    int icnt = 0;//匹配了几个一样的
                                 //循环,如果autoid一样的,更改数量 和货位

                    for (int i = 0; i < dtsum.Rows.Count; i++)
                    {
                        string iautoid2 = DbHelper.GetDbString(dtsum.Rows[i]["SOURCEROWID"]);
                        int ifirst = 0; //第一个匹配的
                        if (iautoid == iautoid2)
                        {

                            if (sBody[j].cinvcode != DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]))
                            {
                                throw new Exception(string.Format("行{0}和出库单存货编码不对应", iautoid2));
                            }
                            //if (!string.IsNullOrEmpty(sBody[j].cbatch) && string.IsNullOrEmpty(model.materialList[i].batchNum) && sBody[j].cbatch != model.materialList[i].batchNum)
                            //{
                            //    throw new Exception(string.Format("行{0}出库批次和出库单不对应", iautoid2));
                            //}

                            if (icnt == 0)
                            {
                                sBody[j].editprop = "M";
                                sBody[j].iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                                sBody[j].cposition = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]); 
                                ifirst = j;
                            }
                            else
                            {
                                //插入新增行

                                LH.Model.ST.Rdrecord11.Body body = new LH.Model.ST.Rdrecord11.Body();
                                //body = sBody[ifirst];
                                body.editprop = "A";
                                //body.autoid = "0";
                                body.irowno = (sBody.Count + 1).ToString();
                                body.cinvcode = sBody[ifirst].cinvcode;
                                body.cbatch = sBody[ifirst].cbatch;
                                body.cmocode = sBody[ifirst].cmocode;
                                body.imoseq = sBody[ifirst].imoseq;
                                body.impoids = sBody[ifirst].impoids;
                                body.invcode = sBody[ifirst].invcode;
                                body.cmassunit = sBody[ifirst].cmassunit;
                                body.imassdate = sBody[ifirst].imassdate;
                                body.dmadedate = sBody[ifirst].dmadedate;
                                body.dvdate = sBody[ifirst].dvdate;
                                body.cexpirationdate = sBody[ifirst].cexpirationdate;
                                body.iexpiratdatecalcu = sBody[ifirst].iexpiratdatecalcu;

                                body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                                body.cposition = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]); 
                                sBody.Add(body);
                            }
                            icnt++;

                        }
                    }
                    if (icnt == 0)
                    {
                        throw new Exception(string.Format("行{0}没有出库确认信息", iautoid));
                    }


                }

                //审核U8
                try
                {
                    //string cRe = DBService.GetUserLogin("1");
                    //if (cRe != "ok")
                    //{
                    //    throw new Exception(cRe);
                    //}
                    LH.Model.ST.Rdrecord11 sEntity = new LH.Model.ST.Rdrecord11(sHead, sBody);


                    sql = string.Format("select id,chandler from rdrecord11 where ccode='{0}'", scanBarcodes.cCode);
                    DataTable dt = DbHelper2.ExecuteTable(sql, conStr);
                    if (dt.Rows.Count > 0)
                    {
                        string iId = DbHelper.GetDbString(dt.Rows[0]["id"]);
                        string cHandler = DbHelper.GetDbString(dt.Rows[0]["chandler"]);
                        if (!string.IsNullOrEmpty(cHandler))
                        {
                            throw new Exception("单据已审核");
                        }


                        string xmlconfig = XmlOperation.GetConfigXML(scanBarcodes.cOrg, scanBarcodes.cUserName, scanBarcodes.cPwd).ToString();
                        string err, cid, ccode;
                        //先修改
                        if (!Rdrecord11DALCO.ModifyRdrecord11(sEntity, xmlconfig, out err, out cid, out ccode))
                        {
                            throw new Exception(err);
                        }
                        //再审核
                        if (!Rdrecord11DALCO.VerifyRdrecord11(iId, xmlconfig, "verify", out err))
                        {
                            throw new Exception(err);
                        }
                        //审核其他出入库单

                        //回写表头自定义项14
                        sql = string.Format("update rdrecord11 set cdefine14='{1}' from rdrecord11 where ccode='{0}'", scanBarcodes.cCode, cParentid);
                        DbHelper2.ExecuteNonQuery(sql, conStr);
                    }


                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }



        /// <summary>
        /// 审核产成品入库单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool ST_VerifyMoIn(PuArrivalBarcode scanBarcodes)
        {

            try
            {
                string conStr = Common.AppSettings.GetAppSeting(scanBarcodes.cOrg);
                if (string.IsNullOrEmpty(conStr))
                {
                    throw new Exception("未配置账套数据库");
                }

                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表WMS_RDRECORDS_ORI.欧瑞京，条码只有存货编码，所以在保存原始数据的时候，把批次，货位一起保存
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                          new SqlParameter("@invcode",scanBarcodes.fenpeilist[i].cInvCode),
                         new SqlParameter("@batch",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cBatch)),
                          new SqlParameter("@poscode",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cPosCode)),
                          new SqlParameter("@whcode",DbHelper.ToDbValue(scanBarcodes.cWhcode)),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                         new SqlParameter("@userid",scanBarcodes.cUserId),
                         new SqlParameter("@vencode",DbHelper.ToDbValue(scanBarcodes.cVenCode)),
                         new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                         new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,invcode,batch,poscode,whcode,sourcerowid,
qty,ctype,cmaker,ddate,userid,vencode,depcode,personcode,ptcode,sourcecode) values(@parentid,@barcodeno,@invcode,@batch,@poscode,@whcode,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@vencode,@depcode,@personcode,@ptcode,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                int ibs = 1;  //数量计算 *1或*-1
                if (scanBarcodes.cType == "生产退库")
                {

                    ibs = -1;
                }
                RetModel retModel = new RetModel();

                String getMainSql = String.Format("select 'M' as editprop,* from RecordInQ with(nolock) where ccode ='{0}'", scanBarcodes.cCode);
                DataTable mainDt = DbHelper2.ExecuteTable(getMainSql, conStr);
                LH.Model.ST.Rdrecord10.Head sHead = LH.U8API.Tool.U8APIHelper.EntityList<LH.Model.ST.Rdrecord10.Head>(mainDt)[0];


                //取表体的信息，汇总. makedate有效期没写。  更新货位信息

                DataTable dtsum = OMS_GetBarcodeDetail(conStr, cParentid);
                //更改货位信息，读取当前的货位信息
                String getDetailSql = String.Format("select a.*,CONVERT(NCHAR,CONVERT(MONEY,b.rowufts),2) as rowufts from RecordInSQ a with(nolock),rdrecords10 b with(nolock) WHERE a.autoid=b.AutoID  and a.id={0}", sHead.id);
                DataTable detailDt = DbHelper2.ExecuteTable(getDetailSql, conStr);
                List<LH.Model.ST.Rdrecord10.Body> sBody = LH.U8API.Tool.U8APIHelper.EntityList<LH.Model.ST.Rdrecord10.Body>(detailDt);
                int bodycnt = sBody.Count;  //读取body数量，避免中间增加改变body值

                for (int j = 0; j < bodycnt; j++)
                {
                    string iautoid = sBody[j].autoid;
                    int icnt = 0;//匹配了几个一样的
                                 //循环,如果autoid一样的,更改数量 和货位

                    for (int i = 0; i < dtsum.Rows.Count; i++)
                    {
                        string iautoid2 = DbHelper.GetDbString(dtsum.Rows[i]["SOURCEROWID"]);
                        int ifirst = 0; //第一个匹配的
                        if (iautoid == iautoid2)
                        {

                            if (sBody[j].cinvcode != DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]))
                            {
                                throw new Exception(string.Format("行{0}和出库单存货编码不对应", iautoid2));
                            }
                            //if (!string.IsNullOrEmpty(sBody[j].cbatch) && string.IsNullOrEmpty(model.materialList[i].batchNum) && sBody[j].cbatch != model.materialList[i].batchNum)
                            //{
                            //    throw new Exception(string.Format("行{0}出库批次和出库单不对应", iautoid2));
                            //}

                            if (icnt == 0)
                            {
                                sBody[j].editprop = "M";
                                sBody[j].iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                                sBody[j].cposition = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]);
                                ifirst = j;
                            }
                            else
                            {
                                //插入新增行

                                LH.Model.ST.Rdrecord10.Body body = new LH.Model.ST.Rdrecord10.Body();
                                //body = sBody[ifirst];
                                body.editprop = "A";
                                //body.autoid = "0";
                                body.irowno = (sBody.Count + 1).ToString();
                                body.cinvcode = sBody[ifirst].cinvcode;
                                body.cbatch = sBody[ifirst].cbatch;
                                body.cmocode = sBody[ifirst].cmocode;
                                body.imoseq = sBody[ifirst].imoseq;

                                body.impoids = sBody[ifirst].impoids;
                                body.cmolotcode = sBody[ifirst].cmolotcode;
                                body.cmassunit = sBody[ifirst].cmassunit;
                                body.imassdate = sBody[ifirst].imassdate;
                                body.dmadedate = sBody[ifirst].dmadedate;
                                body.dvdate = sBody[ifirst].dvdate;
                                body.cexpirationdate = sBody[ifirst].cexpirationdate;
                                body.iexpiratdatecalcu = sBody[ifirst].iexpiratdatecalcu;

                                body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                                body.cposition = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]);
                                sBody.Add(body);
                            }
                            icnt++;

                        }
                    }
                    if (icnt == 0)
                    {
                        throw new Exception(string.Format("行{0}没有出库确认信息", iautoid));
                    }


                }

                //审核U8
                try
                {
                    //string cRe = DBService.GetUserLogin("1");
                    //if (cRe != "ok")
                    //{
                    //    throw new Exception(cRe);
                    //}
                    LH.Model.ST.Rdrecord10 sEntity = new LH.Model.ST.Rdrecord10(sHead, sBody);


                    sql = string.Format("select id,chandler from rdrecord10 where ccode='{0}'", scanBarcodes.cCode);
                    DataTable dt = DbHelper2.ExecuteTable(sql, conStr);
                    if (dt.Rows.Count > 0)
                    {
                        string iId = DbHelper.GetDbString(dt.Rows[0]["id"]);
                        string cHandler = DbHelper.GetDbString(dt.Rows[0]["chandler"]);
                        if (!string.IsNullOrEmpty(cHandler))
                        {
                            throw new Exception("单据已审核");
                        }


                        string xmlconfig = XmlOperation.GetConfigXML(scanBarcodes.cOrg, scanBarcodes.cUserName, scanBarcodes.cPwd).ToString();
                        string err, cid, ccode;
                        //先修改
                        if (!Rdrecord10DALCO.ModifyRdrecord10(sEntity, xmlconfig, out err, out cid, out ccode))
                        {
                            throw new Exception(err);
                        }
                        //再审核
                        if (!Rdrecord10DALCO.VerifyRdrecord10(iId, xmlconfig, "verify", out err))
                        {
                            throw new Exception(err);
                        }
                        //审核其他出入库单

                        //回写表头自定义项14
                        sql = string.Format("update rdrecord10 set cdefine14='{1}' from rdrecord10 where ccode='{0}'", scanBarcodes.cCode, cParentid);
                        DbHelper2.ExecuteNonQuery(sql, conStr);
                    }


                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }



        /// <summary>
        /// 生成货位调整单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool ST_PositionChange(PuArrivalBarcode scanBarcodes)
        {

            try
            {
                string conStr = Common.AppSettings.GetAppSeting(scanBarcodes.cOrg);
                if (string.IsNullOrEmpty(conStr))
                {
                    throw new Exception("未配置账套数据库");
                }

                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表WMS_RDRECORDS_ORI.欧瑞京，条码只有存货编码，所以在保存原始数据的时候，把批次，货位一起保存
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                          new SqlParameter("@invcode",scanBarcodes.fenpeilist[i].cInvCode),
                         new SqlParameter("@batch",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cBatch)),
                          new SqlParameter("@poscode",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cPosCode)),
                            new SqlParameter("@poscode2",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cPosCode2)),
                          new SqlParameter("@whcode",DbHelper.ToDbValue(scanBarcodes.cWhcode)),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                         new SqlParameter("@userid",scanBarcodes.cUserId),
                         new SqlParameter("@vencode",DbHelper.ToDbValue(scanBarcodes.cVenCode)),
                         new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                         new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,invcode,batch,poscode,poscode2,whcode,sourcerowid,
qty,ctype,cmaker,ddate,userid,vencode,depcode,personcode,ptcode,sourcecode) values(@parentid,@barcodeno,@invcode,@batch,@poscode,@poscode2,@whcode,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@vencode,@depcode,@personcode,@ptcode,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }


                RetModel retModel = new RetModel();

                
                //取表体的信息，汇总. makedate有效期没写。  更新货位信息

                DataTable dtsum = OMS_GetBarcodeDetail(conStr, cParentid);
                LH.Model.ST.AdjustPVouch.Head rdHead = new LH.Model.ST.AdjustPVouch.Head();

                rdHead.csource = "1";
                rdHead.dDate = scanBarcodes.dDate.ToString("yyyy-MM-dd");
                rdHead.cVouchCode = "002";
                //rdHead.cMemo = "testCO";
                rdHead.cMaker = scanBarcodes.cUserName;
                rdHead.cWhCode = scanBarcodes.cWhcode;
                //rdHead.chandler = scanBarcodes.cUserName;

                rdHead.iPrintCount = "0";
                //rdHead.dVeriDate = model.ddate;
                //rdHead.cdefine1 = model.purchaseReceiptNo;   //入库单号写自定义项1
                //部门、人员，供应商等，根据表体的订单id取值
                List<LH.Model.ST.AdjustPVouch.Body> bodies = new List<LH.Model.ST.AdjustPVouch.Body>();

                for (int i = 0; i < dtsum.Rows.Count; i++)
                {
                    LH.Model.ST.AdjustPVouch.Body body = new LH.Model.ST.AdjustPVouch.Body();

                    body.cInvCode =DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]);
                    body.iQuantity = DbHelper.GetDbString(dtsum.Rows[i]["QTY"]);
                    body.cBPosCode = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]);
                    body.cAPosCode = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE2"]);
                    body.cBatch = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                    //body.dMadeDate = model.materialList[i].manufactureDate;
                    //body.cAssUnit= model.
                    body.irowno = (i + 1).ToString();
                    bodies.Add(body);
                }

                LH.Model.ST.AdjustPVouch entity = new LH.Model.ST.AdjustPVouch(rdHead, bodies);
                string cRe = GetInfo.GetHWTZInfo(entity, conStr);
                if (cRe != "ok")
                {
                    throw new Exception(cRe);
                }
                //新增
                string cerr, cid, ccode;

                string xmlconfig = XmlOperation.GetConfigXML(scanBarcodes.cOrg, scanBarcodes.cUserName, scanBarcodes.cPwd).ToString();
                if (!AdjustPVouchDALCO.AddVerifyAdjustPVouch(entity, xmlconfig, out cerr, out cid, out ccode))
                {
                    throw new Exception(cerr);
                }

                return DBService.ReOKbool();

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }

        /// <summary>
        /// 审核其他出库单，调拨生成的
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool MaterialOutRD09Verify(ScanBarcode scanBarcodes)
        {

            try
            {
                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                RetModel retModel = new RetModel();
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].barcode),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].quantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",DateTime.Now.ToString("yyyy-MM-dd")),
                          new SqlParameter("@userid",scanBarcodes.cUserId),
                           new SqlParameter("@vencode",DbHelper.ToDbValue(scanBarcodes.cVenCode)),
                            new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                             new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                              new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                               new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,sourcerowid,
qty,ctype,cmaker,ddate,userid,vencode,depcode,personcode,ptcode,sourcecode) values(@parentid,@barcodeno,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@vencode,@depcode,@personcode,@ptcode,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }
                //WMS内审核去掉 

                //审核U8
                try
                {
                    string cRe = DBService.GetUserLogin("1");
                    if (cRe != "ok")
                    {
                        throw new Exception(cRe);
                    }

                     
                    sql = string.Format("select id,chandler from rdrecord09 where ccode='{0}'", scanBarcodes.cCode);
                    DataTable dt = DbHelper2.ExecuteTable(sql, CanShu.conStr);
                    if (dt.Rows.Count > 0)
                    {
                        string iId = DbHelper.GetDbString(dt.Rows[0]["id"]);
                        string cHandler = DbHelper.GetDbString(dt.Rows[0]["chandler"]);
                        if (!string.IsNullOrEmpty(cHandler))
                        {
                            throw new Exception("单据已审核");
                        }



                        string err;
                        if (!Rdrecord09DALCO.VerifyRdrecord09(iId, "verify", out err))
                        {
                            throw new Exception(err);
                        }

                          
                    }


                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }



        /// <summary>
        /// 插入扫描的原始条码
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool SA_DispToRd32(ScanBarcodeSA scanBarcodes)
        {

            try
            {
                string conStr = Common.AppSettings.GetAppSeting(scanBarcodes.cOrg);
                if (string.IsNullOrEmpty(conStr))
                {
                    throw new Exception("未配置账套数据库");
                }
                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                RetModel retModel = new RetModel();
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {

                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                          new SqlParameter("@invcode",scanBarcodes.fenpeilist[i].cInvCode),
                         new SqlParameter("@batch",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cBatch)),
                          new SqlParameter("@poscode",DbHelper.ToDbValue(scanBarcodes.fenpeilist[i].cPosCode)),
                          new SqlParameter("@whcode",DbHelper.ToDbValue(scanBarcodes.cWhcode)),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                         new SqlParameter("@userid",scanBarcodes.cUserId),
                         new SqlParameter("@cuscode",DbHelper.ToDbValue(scanBarcodes.cCusCode)),
                         new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cPtCode)),
                         new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };

                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,invcode,batch,poscode,whcode,sourcerowid,
qty,ctype,cmaker,ddate,userid,cuscode,depcode,personcode,ptcode,sourcecode) values(@parentid,@barcodeno,@invcode,@batch,@poscode,@whcode,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@cuscode,@depcode,@personcode,@ptcode,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);
                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }
 
                string cRdcode = GetRdcode(conStr, scanBarcodes.cType);
                string cCode = GetNewcode(conStr, "rdrecord32");

                int iRdflag = 0;
                int ibs = 1;  //数量计算 *1或*-1
                if (scanBarcodes.cType == "销售退货")
                {
                    iRdflag = 1;
                    ibs = -1;
                }
                //取表体的信息，汇总. makedate有效期没写

                DataTable dtsum = OMS_GetBarcodeDetail(conStr, cParentid);
                 
                //插入U8
                try
                { 

                        LH.Model.ST.Rdrecord32.Head rdHead = new LH.Model.ST.Rdrecord32.Head();

                        rdHead.cvouchtype = "32";
                        rdHead.crdcode = cRdcode;
                        rdHead.cbustype = "普通销售";
                        rdHead.csource = "发货单";
                        rdHead.cwhcode = scanBarcodes.cWhcode;
                        rdHead.ddate = scanBarcodes.dDate.ToString("yyyy-MM-dd");
                        //rdHead.cdepcode = "0401";
                        //rdHead.cvencode = "111";
                        rdHead.cmaker = scanBarcodes.cUserName;
                        //rdHead.chandler = model.verifier;
                        //rdHead.vt_id = "27";
                        //rdHead.itaxrate = "13.00";
                        //rdHead.iexchrate = "1";
                        //rdHead.cexch_name = "人民币"; 
                        //rdHead.cdefine1 = "顾潇";
                        rdHead.brdflag = "0";
                        //rdHead.cpersoncode = "00043";
                        //rdHead.cordercode = "0000000052";
                        //rdHead.ipurorderid = "1000000054";
                        rdHead.iPrintCount = "0";
                        rdHead.ccode = cCode;
                        rdHead.cdefine14 = cParentid;   //入库单号写自定义项1
                                                       //部门、人员，供应商等，根据表体的订单id取值


                        List<LH.Model.ST.Rdrecord32.Body> bodies = new List<LH.Model.ST.Rdrecord32.Body>();

                        for (int i = 0; i < dtsum.Rows.Count; i++)
                        {
                            LH.Model.ST.Rdrecord32.Body body = new LH.Model.ST.Rdrecord32.Body();
                            body.cinvcode = DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]);
                            body.cbatch = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                            //body.cposition = model.materialList[i].cposcode;
                            body.idlsid = DbHelper.GetDbString(dtsum.Rows[i]["SOURCEROWID"]);
                            //body.dmadedate = model.materialList[i].manufactureDate;
                            body.cposition = DbHelper.GetDbString(dtsum.Rows[i]["POSCODE"]);
                        body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                            //body.inum = model.materialList[i].count;
                            body.editprop = "A";
                            //if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["MAKEDATE"])))
                            //{
                            //    body.dmadedate = DbHelper.GetDbDate(dtsum.Rows[i]["MAKEDATE"]).ToString("yyyy-MM-dd");
                            //}
                            body.irowno = (i + 1).ToString();


                            bodies.Add(body);
                        }



                        LH.Model.ST.Rdrecord32 entity = new LH.Model.ST.Rdrecord32(rdHead, bodies);
                        string cRe = GetInfo.GetST32ByDispInfo(entity, conStr);
                        if (cRe != "ok")
                        {
                            throw new Exception(cRe);
                        }
                        //新增
                        string cerr, cid, ccode;
                    string xmlconfig = XmlOperation.GetConfigXML(scanBarcodes.cOrg, scanBarcodes.cUserName, scanBarcodes.cPwd).ToString();
                    if (!Rdrecord32DALCO.AddVerifyRdrecord32(entity, xmlconfig, out cerr, out cid, out ccode))
                        {
                            throw new Exception(cerr);
                        }

                        //MessageBox.Show(id);
                        //审核
                        //LH.U8API.Bus.Pu_ArrivalVouchDAL.ConfirmArrivalVouch(id, con, u8login, true, true);
                        //MessageBox.Show("审核成功");
                        


                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }

       


        /// <summary>
        /// 插入其他出库单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool OtherOutRD09(PuArrivalBarcode scanBarcodes)
        {

            try
            {
                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                RetModel retModel = new RetModel();
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",DateTime.Now.ToString("yyyy-MM-dd")),
                          new SqlParameter("@userid",scanBarcodes.cUserId),                       
                            //new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                            // new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                            //  new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cStCode)),
                               new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,sourcerowid,
qty,ctype,cmaker,ddate,userid,sourcecode) values(@parentid,@barcodeno,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }
                            
                //生成新的单据号
                string cVouchCode = GetNewcode(conStr, "WMS_RDRECORD");
                string cRdcode = GetRdcode(conStr, scanBarcodes.cType);

                int iRdflag = 1;
                int ibs = 1;  //数量计算 *1或*-1

                //取表体的信息，汇总. makedate有效期没写
                DataTable dtsum = GetBarcodeDetail(cParentid);
                if (dtsum.Rows.Count == 0)
                {
                    throw new Exception("条码无法关联到存货编码");
                }
                tran = DbHelper2.BeginTrans(conStr);
                try
                {
                    SqlParameter[] param2 = new SqlParameter[]{
                        new SqlParameter("@ID",cParentid),
                         new SqlParameter("@CCODE",cVouchCode),
                         new SqlParameter("@DDATE",DateTime.Now.ToString("yyyy-MM-dd")),
                         new SqlParameter("@RDFLAG",iRdflag),
                          new SqlParameter("@RDCODE",DbHelper.ToDbValue(cRdcode)),
                         new SqlParameter("@WHCODE",scanBarcodes.cWhcode),
                         //new SqlParameter("@DEPCODE",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         //new SqlParameter("@PERSONCODE",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         // new SqlParameter("@CUSCODE",DbHelper.ToDbValue(scanBarcodes.cCusCode)),
                         // new SqlParameter("@PTCODE",DbHelper.ToDbValue(scanBarcodes.cStCode)),
                          new SqlParameter("@SOURCECODE",DbHelper.ToDbValue(scanBarcodes.cCode)),
                          new SqlParameter("@CREATEUSERID",scanBarcodes.cUserId),
                          new SqlParameter("@CREATEBY",scanBarcodes.cUserName),
                           new SqlParameter("@vouchtype","其他出库")
          };

                    sql = string.Format(@"INSERT INTO [dbo].[WMS_RDRECORD]
           ([ID]
           ,[CCODE]
           ,[DDATE]
           ,[RDCODE]
           ,[RDFLAG]
           ,[WHCODE]
           ,[SOURCECODE]
           ,[DELETEMARK]
           ,[CREATEON]
           ,[CREATEUSERID]
           ,[CREATEBY],vouchtype) values(@ID
           ,@CCODE
           ,@DDATE
           ,@RDCODE
           ,@RDFLAG
           ,@WHCODE
           ,@SOURCECODE
           ,0
           ,GETDATE()
           ,@CREATEUSERID
           ,@CREATEBY,@vouchtype)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param2, tran);



                    for (int i = 0; i < dtsum.Rows.Count; i++)
                    {
                        SqlParameter[] param3 = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@INVCODE",dtsum.Rows[i]["INVCODE"]),
                         new SqlParameter("@UNIT",dtsum.Rows[i]["UNITNAME"]),
                         new SqlParameter("@ROWNO",(i+1).ToString()),
                         new SqlParameter("@BATCH",dtsum.Rows[i]["BATCH"]),
                         new SqlParameter("@QUANTITY",DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"])*ibs),
                         new SqlParameter("@MAKEDATE",DbHelper.ToDbValue(dtsum.Rows[i]["MAKEDATE"])),
                          new SqlParameter("@DELETEMARK","0"),
                           new SqlParameter("@SOURCEROWID",DbHelper.ToDbValue(dtsum.Rows[i]["SOURCEROWID"]))

          };
                        sql = @"insert into WMS_RDRECORDS([ID]
           ,[PARENTID]
           ,[ROWNO]
           ,[INVCODE]
           ,[UNIT]
           ,[BATCH]
           ,[QUANTITY]
           ,[MAKEDATE]
           ,[DELETEMARK]
           ,[SOURCEROWID]) values(newid(),@PARENTID
           ,@ROWNO
           ,@INVCODE
           ,@UNIT
           ,@BATCH
           ,@QUANTITY
           ,@MAKEDATE
           ,@DELETEMARK
           ,@SOURCEROWID)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param3, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                //插入U8
                try
                {
                     
                        LH.Model.ST.Rdrecord09.Head rdHead = new LH.Model.ST.Rdrecord09.Head();

                        rdHead.cvouchtype = "09";
                        rdHead.crdcode = cRdcode;
                        //rdHead.cbustype = "普通采购";
                        rdHead.csource = "库存";
                        rdHead.cwhcode = scanBarcodes.cWhcode;
                        rdHead.ddate = DateTime.Now.ToString("yyyy-MM-dd");
                        //rdHead.cdepcode = "0401";
                        //rdHead.cvencode = "111";
                        rdHead.cmaker = scanBarcodes.cUserName;
                        //rdHead.chandler = model.verifier;
                        //rdHead.vt_id = "27";
                        //rdHead.itaxrate = "13.00";
                        //rdHead.iexchrate = "1";
                        //rdHead.cexch_name = "人民币"; 
                        //rdHead.cdefine1 = "顾潇";
                        rdHead.brdflag = "0";
                        //rdHead.cpersoncode = "00043";
                        //rdHead.cordercode = "0000000052";
                        //rdHead.ipurorderid = "1000000054";
                        rdHead.iPrintCount = "0";
                        rdHead.ccode = "WMS" + cVouchCode;
                        rdHead.cdefine10 = cParentid;   //入库单号写自定义项1
                                                       //部门、人员，供应商等，根据表体的订单id取值


                        List<LH.Model.ST.Rdrecord09.Body> bodies = new List<LH.Model.ST.Rdrecord09.Body>();

                        for (int i = 0; i < dtsum.Rows.Count; i++)
                        {
                            LH.Model.ST.Rdrecord09.Body body = new LH.Model.ST.Rdrecord09.Body();
                            body.cinvcode = DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]);
                            if(!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["BATCH"])))
                            {
                                body.cbatch = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                            }
                            
                            //body.cposition = model.materialList[i].cposcode;
                            //body.dmadedate = model.materialList[i].manufactureDate;
                            body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                            //body.inum = model.materialList[i].count;
                            body.editprop = "A";
                            if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["MAKEDATE"])))
                            {
                                body.dmadedate = DbHelper.GetDbDate(dtsum.Rows[i]["MAKEDATE"]).ToString("yyyy-MM-dd");
                            }
                            body.irowno = (i + 1).ToString();


                            bodies.Add(body);
                        }



                        LH.Model.ST.Rdrecord09 entity = new LH.Model.ST.Rdrecord09(rdHead, bodies);
                        string cRe = GetInfo.GetST09Info(entity, CanShu.conStr);
                        if (cRe != "ok")
                        {
                            throw new Exception(cRe);
                        }
                    string cerr, cid, ccode;
                    if (!Rdrecord09DALCO.AddRdrecord09(entity, out cerr, out cid, out ccode))
                    {
                        throw new Exception(cerr);
                    }

                    //MessageBox.Show(id);
                    //审核
                    //LH.U8API.Bus.Pu_ArrivalVouchDAL.ConfirmArrivalVouch(id, con, u8login, true, true);
                    //MessageBox.Show("审核成功");

                    //更改条码的状态
                    BarcodeManager barcodeManager = new BarcodeManager();
                    barcodeManager.ChangeBarcodestate(cParentid, "已出库");

                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    //如果有错误，删除之前生成的信息
                    DelVouch(conStr, cParentid);
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }


        /// <summary>
        /// 使用co的webservice
        /// </summary>
        /// <param name="scanBarcodes"></param>
        /// <returns></returns>
        public static RetModelbool OtherInRD08(PuArrivalBarcode scanBarcodes)
        {

            try
            {
                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                RetModel retModel = new RetModel();
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                                        new SqlParameter("@parentid",cParentid),
                                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                                         new SqlParameter("@ctype",scanBarcodes.cType ),
                                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                                         new SqlParameter("@ddate",scanBarcodes.dDate),
                                          new SqlParameter("@userid",scanBarcodes.cUserId),                       
                                            //new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                                            // new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                                            //  new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cStCode)),
                                               new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
                          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,sourcerowid,
                qty,ctype,cmaker,ddate,userid,sourcecode) values(@parentid,@barcodeno,@sourcerowid,
                @qty,@ctype,@cmaker,@ddate,@userid,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                //生成新的单据号
                string cVouchCode = GetNewcode(conStr, "WMS_RDRECORD");
                string cRdcode = GetRdcode(conStr, scanBarcodes.cType);

                int iRdflag = 1;
                int ibs = 1;  //数量计算 *1或*-1

                //取表体的信息，汇总. makedate有效期没写

                DataTable dtsum = GetBarcodeDetail(cParentid);
                if (dtsum.Rows.Count == 0)
                {
                    throw new Exception("条码无法关联到存货编码");
                }
                tran = DbHelper2.BeginTrans(conStr);
                try
                {
                    SqlParameter[] param2 = new SqlParameter[]{
                                        new SqlParameter("@ID",cParentid),
                                         new SqlParameter("@CCODE",cVouchCode),
                                         new SqlParameter("@DDATE",scanBarcodes.dDate),
                                         new SqlParameter("@RDFLAG",iRdflag),
                                          new SqlParameter("@RDCODE",DbHelper.ToDbValue(cRdcode)),
                                         new SqlParameter("@WHCODE",scanBarcodes.cWhcode),
                                         //new SqlParameter("@DEPCODE",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                                         //new SqlParameter("@PERSONCODE",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                                         // new SqlParameter("@CUSCODE",DbHelper.ToDbValue(scanBarcodes.cCusCode)),
                                         // new SqlParameter("@PTCODE",DbHelper.ToDbValue(scanBarcodes.cStCode)),
                                          new SqlParameter("@SOURCECODE",DbHelper.ToDbValue(scanBarcodes.cCode)),
                                          new SqlParameter("@CREATEUSERID",scanBarcodes.cUserId),
                                          new SqlParameter("@CREATEBY",scanBarcodes.cUserName),
                                           new SqlParameter("@vouchtype","其他入库")
                          };

                    sql = string.Format(@"INSERT INTO [dbo].[WMS_RDRECORD]
                           ([ID]
                           ,[CCODE]
                           ,[DDATE]
                           ,[RDCODE]
                           ,[RDFLAG]
                           ,[WHCODE]
                           ,[SOURCECODE]
                           ,[DELETEMARK]
                           ,[CREATEON]
                           ,[CREATEUSERID]
                           ,[CREATEBY],vouchtype) values(@ID
                           ,@CCODE
                           ,@DDATE
                           ,@RDCODE
                           ,@RDFLAG
                           ,@WHCODE
                           ,@SOURCECODE
                           ,0
                           ,GETDATE()
                           ,@CREATEUSERID
                           ,@CREATEBY,@vouchtype)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param2, tran);



                    for (int i = 0; i < dtsum.Rows.Count; i++)
                    {
                        SqlParameter[] param3 = new SqlParameter[]{
                                        new SqlParameter("@parentid",cParentid),
                                         new SqlParameter("@INVCODE",dtsum.Rows[i]["INVCODE"]),
                                         new SqlParameter("@UNIT",dtsum.Rows[i]["UNITNAME"]),
                                         new SqlParameter("@ROWNO",(i+1).ToString()),
                                         new SqlParameter("@BATCH",dtsum.Rows[i]["BATCH"]),
                                         new SqlParameter("@QUANTITY",DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"])*ibs),
                                         new SqlParameter("@MAKEDATE",DbHelper.ToDbValue(dtsum.Rows[i]["MAKEDATE"])),
                                          new SqlParameter("@DELETEMARK","0"),
                                           new SqlParameter("@SOURCEROWID",DbHelper.ToDbValue(dtsum.Rows[i]["SOURCEROWID"]))

                          };
                        sql = @"insert into WMS_RDRECORDS([ID]
                           ,[PARENTID]
                           ,[ROWNO]
                           ,[INVCODE]
                           ,[UNIT]
                           ,[BATCH]
                           ,[QUANTITY]
                           ,[MAKEDATE]
                           ,[DELETEMARK]
                           ,[SOURCEROWID]) values(newid(),@PARENTID
                           ,@ROWNO
                           ,@INVCODE
                           ,@UNIT
                           ,@BATCH
                           ,@QUANTITY
                           ,@MAKEDATE
                           ,@DELETEMARK
                           ,@SOURCEROWID)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param3, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                //插入U8
                try
                { 
                        LH.Model.ST.Rdrecord08.Head rdHead = new LH.Model.ST.Rdrecord08.Head();

                        rdHead.cvouchtype = "08";
                        //rdHead.crdcode = cRdcode;
                        rdHead.cbustype = "其他入库";
                        rdHead.csource = "库存";
                        rdHead.cwhcode = scanBarcodes.cWhcode;
                        rdHead.ddate = scanBarcodes.dDate.ToString("yyyy-MM-dd");
                        //rdHead.cdepcode = "0401";
                        //rdHead.cvencode = "111";
                        rdHead.cmaker = scanBarcodes.cUserName;
                        //rdHead.chandler = model.verifier;
                        //rdHead.vt_id = "27";
                        //rdHead.itaxrate = "13.00";
                        //rdHead.iexchrate = "1";
                        //rdHead.cexch_name = "人民币"; 
                        //rdHead.cdefine1 = "顾潇";
                        rdHead.brdflag = "1";
                        //rdHead.cpersoncode = "00043";
                        //rdHead.cordercode = "0000000052";
                        //rdHead.ipurorderid = "1000000054";
                        rdHead.iPrintCount = "0";
                        rdHead.ccode = "WMS" + cVouchCode;
                        rdHead.cdefine10 = cParentid;   //入库单号写自定义项1
                                                        //部门、人员，供应商等，根据表体的订单id取值


                        List<LH.Model.ST.Rdrecord08.Body> bodies = new List<LH.Model.ST.Rdrecord08.Body>();

                        for (int i = 0; i < dtsum.Rows.Count; i++)
                        {
                            LH.Model.ST.Rdrecord08.Body body = new LH.Model.ST.Rdrecord08.Body();
                            body.cinvcode = DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]);
                            body.cbatch = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                        //body.cposition = model.materialList[i].cposcode;
                       
                        body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                        //body.inum = model.materialList[i].count;
                        
                            body.editprop = "A";
                            if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["MAKEDATE"])))
                            {
                                body.dmadedate = DbHelper.GetDbDate(dtsum.Rows[i]["MAKEDATE"]).ToString("yyyy-MM-dd");
                            }
                            body.irowno = (i + 1).ToString();


                            bodies.Add(body);
                        }



                        LH.Model.ST.Rdrecord08 entity = new LH.Model.ST.Rdrecord08(rdHead, bodies);
                        string cRe = GetInfo.GetST08Info(entity, CanShu.conStr);
                        if (cRe != "ok")
                        {
                            throw new Exception(cRe);
                        }
                    string cerr,cid,ccode;  
                    if (!Rdrecord08DALCO.AddRdrecord08(entity, out cerr, out cid, out ccode))
                    {
                        throw new Exception(cerr);
                    }


                    //更改条码的状态
                    BarcodeManager barcodeManager = new BarcodeManager();
                    barcodeManager.ChangeBarcodestate(cParentid, "在库");

                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    //如果有错误，删除之前生成的信息
                    DelVouch(conStr, cParentid);
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }


       


        /// <summary>
        /// 插入材料出库单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool MaterialOutRD11(PuArrivalBarcode scanBarcodes)
        {

            try
            {
                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                RetModel retModel = new RetModel();
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].cBarcode),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].iQuantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                          new SqlParameter("@userid",scanBarcodes.cUserId),                       
                            //new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                            // new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                            //  new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cStCode)),
                          new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,sourcerowid,
qty,ctype,cmaker,ddate,userid,sourcecode) values(@parentid,@barcodeno,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid,@sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }
              
                string cVouchCode = GetNewcode(conStr, "WMS_RDRECORD");
                string cRdcode = GetRdcode(conStr, scanBarcodes.cType);

                int iRdflag = 1;
                int ibs = 1;  //数量计算 *1或*-1


                DataTable dtsum = GetBarcodeDetail(cParentid);
                if (dtsum.Rows.Count == 0)
                {
                    throw new Exception("条码无法关联到存货编码");
                }
                tran = DbHelper2.BeginTrans(conStr);
                try
                {
                    SqlParameter[] param2 = new SqlParameter[]{
                        new SqlParameter("@ID",cParentid),
                         new SqlParameter("@CCODE",cVouchCode),
                         new SqlParameter("@DDATE",scanBarcodes.dDate),
                         new SqlParameter("@RDFLAG",iRdflag),
                          new SqlParameter("@RDCODE",DbHelper.ToDbValue(cRdcode)),
                         new SqlParameter("@WHCODE",scanBarcodes.cWhcode),
                         //new SqlParameter("@DEPCODE","D61"),
                         //new SqlParameter("@PERSONCODE",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         // new SqlParameter("@CUSCODE",DbHelper.ToDbValue(scanBarcodes.cCusCode)),
                         // new SqlParameter("@PTCODE",DbHelper.ToDbValue(scanBarcodes.cStCode)),
                          new SqlParameter("@SOURCECODE",DbHelper.ToDbValue(scanBarcodes.cCode)),
                          new SqlParameter("@CREATEUSERID",scanBarcodes.cUserId),
                          new SqlParameter("@CREATEBY",scanBarcodes.cUserName),
                           new SqlParameter("@vouchtype","材料出库")
          };

                    sql = string.Format(@"INSERT INTO [dbo].[WMS_RDRECORD]
           ([ID]
           ,[CCODE]
           ,[DDATE]
           ,[RDCODE]
           ,[RDFLAG]
           ,[WHCODE]
           ,[SOURCECODE]
           ,[DELETEMARK]
           ,[CREATEON]
           ,[CREATEUSERID]
           ,[CREATEBY],vouchtype) values(@ID
           ,@CCODE
           ,@DDATE
           ,@RDCODE
           ,@RDFLAG
           ,@WHCODE
           ,@SOURCECODE
           ,0
           ,GETDATE()
           ,@CREATEUSERID
           ,@CREATEBY,@vouchtype)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param2, tran);



                    for (int i = 0; i < dtsum.Rows.Count; i++)
                    {
                        SqlParameter[] param3 = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@INVCODE",dtsum.Rows[i]["INVCODE"]),
                         new SqlParameter("@UNIT",dtsum.Rows[i]["UNITNAME"]),
                         new SqlParameter("@ROWNO",(i+1).ToString()),
                         new SqlParameter("@BATCH",dtsum.Rows[i]["BATCH"]),
                         new SqlParameter("@QUANTITY",DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"])*ibs),
                         new SqlParameter("@MAKEDATE",DbHelper.ToDbValue(dtsum.Rows[i]["MAKEDATE"])),
                          new SqlParameter("@DELETEMARK","0"),
                           new SqlParameter("@SOURCEROWID",DbHelper.ToDbValue(dtsum.Rows[i]["SOURCEROWID"]))

          };
                        sql = @"insert into WMS_RDRECORDS([ID]
           ,[PARENTID]
           ,[ROWNO]
           ,[INVCODE]
           ,[UNIT]
           ,[BATCH]
           ,[QUANTITY]
           ,[MAKEDATE]
           ,[DELETEMARK]
           ,[SOURCEROWID]) values(newid(),@PARENTID
           ,@ROWNO
           ,@INVCODE
           ,@UNIT
           ,@BATCH
           ,@QUANTITY
           ,@MAKEDATE
           ,@DELETEMARK
           ,@SOURCEROWID)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param3, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                //插入U8
                try
                {
                    string cRe = DBService.GetUserLogin("1");
                    if (cRe != "ok")
                    {
                        throw new Exception(cRe);
                    }

                    
                        LH.Model.ST.Rdrecord11.Head rdHead = new LH.Model.ST.Rdrecord11.Head();

                        rdHead.cvouchtype = "11";
                        rdHead.crdcode = cRdcode;
                        rdHead.cbustype = "领料";
                        rdHead.csource = "库存";
                        rdHead.cwhcode = scanBarcodes.cWhcode;
                        rdHead.ddate = scanBarcodes.dDate.ToString("yyyy-MM-dd");
                        rdHead.cdepcode = "D61";
                        //rdHead.cvencode = "111";
                        rdHead.cmaker = scanBarcodes.cUserName;
                        //rdHead.chandler = model.verifier;
                        //rdHead.vt_id = "27";
                        //rdHead.itaxrate = "13.00";
                        //rdHead.iexchrate = "1";
                        //rdHead.cexch_name = "人民币"; 
                        //rdHead.cdefine1 = "顾潇";
                        rdHead.brdflag = "0";
                        //rdHead.cpersoncode = "00043";
                        //rdHead.cordercode = "0000000052";
                        //rdHead.ipurorderid = "1000000054";
                        rdHead.iPrintCount = "0";
                        rdHead.ccode = "WMS" + cVouchCode;
                        rdHead.cdefine10 = cParentid;   //入库单号写自定义项1
                                                       //部门、人员，供应商等，根据表体的订单id取值


                        List<LH.Model.ST.Rdrecord11.Body> bodies = new List<LH.Model.ST.Rdrecord11.Body>();

                        for (int i = 0; i < dtsum.Rows.Count; i++)
                        {
                            LH.Model.ST.Rdrecord11.Body body = new LH.Model.ST.Rdrecord11.Body();
                            body.cinvcode = DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]);
                            body.cbatch = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                            //body.cposition = model.materialList[i].cposcode;
                            //body.dmadedate = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                            body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                            //body.inum = model.materialList[i].count;
                            body.editprop = "A";
                            if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["MAKEDATE"])))
                            {
                                body.dmadedate = DbHelper.GetDbDate(dtsum.Rows[i]["MAKEDATE"]).ToString("yyyy-MM-dd");
                            }
                            body.irowno = (i + 1).ToString();


                            bodies.Add(body);
                        }



                        LH.Model.ST.Rdrecord11 entity = new LH.Model.ST.Rdrecord11(rdHead, bodies);
                        cRe = GetInfo.GetST11Info(entity, CanShu.conStr);
                        if (cRe != "ok")
                        {
                            throw new Exception(cRe);
                        }
                        //新增
                        string cerr, cid, ccode;
                        if (!Rdrecord11DALCO.AddRdrecord11(entity, out cerr, out cid, out ccode))
                        {
                            throw new Exception(cerr);
                        }


                        //MessageBox.Show(id);
                        //审核
                        //LH.U8API.Bus.Pu_ArrivalVouchDAL.ConfirmArrivalVouch(id, con, u8login, true, true);
                        //MessageBox.Show("审核成功");
                        

                    //更改条码的状态
                    BarcodeManager barcodeManager = new BarcodeManager();
                    barcodeManager.ChangeBarcodestate(cParentid, "已材料领用");

                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    //如果有错误，删除之前生成的信息
                    DelVouch(conStr, cParentid);
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }


        /// <summary>
        /// 插入产成品入库单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool MoToRd10(ScanBarcodeMo scanBarcodes)
        {

            try
            {
                if (scanBarcodes.fenpeilist.Count == 0)
                {
                    return DBService.ReErrorbool("没有数据，无法保存");
                }
                RetModel retModel = new RetModel();
                string cParentid = System.Guid.NewGuid().ToString();
                string sql = "";
                SqlTransaction tran = DbHelper2.BeginTrans(conStr);
                //保存到原始表
                try
                {
                    for (int i = 0; i < scanBarcodes.fenpeilist.Count; i++)
                    {
                        SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@barcodeno",scanBarcodes.fenpeilist[i].barcode),
                         new SqlParameter("@sourcerowid",scanBarcodes.fenpeilist[i].autoid),
                         new SqlParameter("@qty",scanBarcodes.fenpeilist[i].quantity),
                         new SqlParameter("@ctype",scanBarcodes.cType ),
                         new SqlParameter("@cmaker",scanBarcodes.cUserName),
                         new SqlParameter("@ddate",scanBarcodes.dDate),
                          new SqlParameter("@userid",scanBarcodes.cUserId),                       
                            //new SqlParameter("@depcode",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                            // new SqlParameter("@personcode",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                            //  new SqlParameter("@ptcode",DbHelper.ToDbValue(scanBarcodes.cStCode)),
                               new SqlParameter("@sourcecode",DbHelper.ToDbValue(scanBarcodes.cCode))
          };
                        sql = @"insert into WMS_RDRECORDS_ORI(parentid,barcodeno,sourcerowid,
qty,ctype,cmaker,ddate,userid,  sourcecode) values(@parentid,@barcodeno,@sourcerowid,
@qty,@ctype,@cmaker,@ddate,@userid, @sourcecode)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                //生成新的单据号
                string cVouchCode = GetNewcode(conStr,"WMS_RDRECORD");
                string cRdcode = GetRdcode(conStr, scanBarcodes.cType);
                int iRdflag = 1;
                int ibs = 1;  //数量计算 *1或*-1
                if (scanBarcodes.cType == "生产退货")
                {
                    iRdflag = 0;
                    ibs = -1;
                }
                //取表体的信息，汇总. makedate有效期没写

                DataTable dtsum = GetBarcodeDetail(cParentid);
                if (dtsum.Rows.Count == 0)
                {
                    throw new Exception("条码无法关联到存货编码");
                }
                tran = DbHelper2.BeginTrans(conStr);
                try
                {
                    SqlParameter[] param2 = new SqlParameter[]{
                        new SqlParameter("@ID",cParentid),
                         new SqlParameter("@CCODE",cVouchCode),
                         new SqlParameter("@DDATE",scanBarcodes.dDate ),
                         new SqlParameter("@RDFLAG",iRdflag),
                          new SqlParameter("@RDCODE",DbHelper.ToDbValue(cRdcode)),
                         new SqlParameter("@WHCODE",scanBarcodes.cwhcode),
                         //new SqlParameter("@DEPCODE",DbHelper.ToDbValue(scanBarcodes.cDepCode)),
                         //new SqlParameter("@PERSONCODE",DbHelper.ToDbValue(scanBarcodes.cPersonCode)),
                         // new SqlParameter("@CUSCODE",DbHelper.ToDbValue(scanBarcodes.cCusCode)),
                         // new SqlParameter("@PTCODE",DbHelper.ToDbValue(scanBarcodes.cStCode)),
                          new SqlParameter("@SOURCECODE",DbHelper.ToDbValue(scanBarcodes.cCode)),
                          new SqlParameter("@CREATEUSERID",scanBarcodes.cUserId),
                          new SqlParameter("@CREATEBY",scanBarcodes.cUserName),
                           new SqlParameter("@vouchtype","产成品入库")
          };

                    sql = string.Format(@"INSERT INTO [dbo].[WMS_RDRECORD]
           ([ID]
           ,[CCODE]
           ,[DDATE]
           ,[RDCODE]
           ,[RDFLAG]
           ,[WHCODE]          
           ,[SOURCECODE]
           ,[DELETEMARK]
           ,[CREATEON]
           ,[CREATEUSERID]
           ,[CREATEBY],vouchtype) values(@ID
           ,@CCODE
           ,@DDATE
           ,@RDCODE
           ,@RDFLAG
           ,@WHCODE          
           ,@SOURCECODE
           ,0
           ,GETDATE()
           ,@CREATEUSERID
           ,@CREATEBY,@vouchtype)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param2, tran);



                    for (int i = 0; i < dtsum.Rows.Count; i++)
                    {
                        SqlParameter[] param3 = new SqlParameter[]{
                        new SqlParameter("@parentid",cParentid),
                         new SqlParameter("@INVCODE",dtsum.Rows[i]["INVCODE"]),
                         new SqlParameter("@UNIT",dtsum.Rows[i]["UNITNAME"]),
                         new SqlParameter("@ROWNO",(i+1).ToString()),
                         new SqlParameter("@BATCH",dtsum.Rows[i]["BATCH"]),
                         new SqlParameter("@QUANTITY",DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"])*ibs),
                         new SqlParameter("@MAKEDATE",DbHelper.ToDbValue(dtsum.Rows[i]["MAKEDATE"])),
                          new SqlParameter("@DELETEMARK","0"),
                           new SqlParameter("@SOURCEROWID",DbHelper.ToDbValue(dtsum.Rows[i]["SOURCEROWID"]))

          };
                        sql = @"insert into WMS_RDRECORDS([ID]
           ,[PARENTID]
           ,[ROWNO]
           ,[INVCODE]
           ,[UNIT]
           ,[BATCH]
           ,[QUANTITY]
           ,[MAKEDATE]
           ,[DELETEMARK]
           ,[SOURCEROWID]) values(newid(),@PARENTID
           ,@ROWNO
           ,@INVCODE
           ,@UNIT
           ,@BATCH
           ,@QUANTITY
           ,@MAKEDATE
           ,@DELETEMARK
           ,@SOURCEROWID)";
                        DbHelper2.ExecuteSqlWithTrans(sql, param3, tran);

                    }
                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }

                //插入U8
                try
                {
                    string cRe = DBService.GetUserLogin("1");
                    if (cRe != "ok")
                    {
                        throw new Exception(cRe);
                    }

                     
                        LH.Model.ST.Rdrecord10.Head rdHead = new LH.Model.ST.Rdrecord10.Head();

                        rdHead.cvouchtype = "10";
                        rdHead.crdcode = cRdcode;
                        rdHead.cbustype = "成品入库";
                        rdHead.csource = "生产订单";
                        rdHead.cwhcode = scanBarcodes.cwhcode;
                        rdHead.ddate = scanBarcodes.dDate.ToString("yyyy-MM-dd");
                        //rdHead.cdepcode = "0401";
                        //rdHead.cvencode = "111";
                        rdHead.cmaker = scanBarcodes.cUserName;
                        //rdHead.chandler = model.verifier;
                        //rdHead.vt_id = "27";
                        //rdHead.itaxrate = "13.00";
                        //rdHead.iexchrate = "1";
                        //rdHead.cexch_name = "人民币"; 
                        //rdHead.cdefine1 = "顾潇";
                        rdHead.brdflag = "0";
                        //rdHead.cpersoncode = "00043";
                        //rdHead.cordercode = "0000000052";
                        //rdHead.ipurorderid = "1000000054";
                        rdHead.iPrintCount = "0";
                        rdHead.ccode = "WMS" + cVouchCode;
                        rdHead.cdefine10 = cParentid;   //入库单号写自定义项10
                                                       //部门、人员，供应商等，根据表体的订单id取值


                        List<LH.Model.ST.Rdrecord10.Body> bodies = new List<LH.Model.ST.Rdrecord10.Body>();

                        for (int i = 0; i < dtsum.Rows.Count; i++)
                        {
                            LH.Model.ST.Rdrecord10.Body body = new LH.Model.ST.Rdrecord10.Body();
                            body.cinvcode = DbHelper.GetDbString(dtsum.Rows[i]["INVCODE"]);
                            body.cbatch = DbHelper.GetDbString(dtsum.Rows[i]["BATCH"]);
                            //body.cposition = model.materialList[i].cposcode;
                            body.impoids = DbHelper.GetDbString(dtsum.Rows[i]["SOURCEROWID"]);
                            //body.dmadedate = model.materialList[i].manufactureDate;
                            body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(dtsum.Rows[i]["QTY"]) * ibs);
                            //body.inum = model.materialList[i].count;
                            if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsum.Rows[i]["MAKEDATE"])))
                            {
                                body.dmadedate = DbHelper.GetDbDate(dtsum.Rows[i]["MAKEDATE"]).ToString("yyyy-MM-dd");
                            }
                            
                            body.editprop = "A";
                            body.irowno = (i + 1).ToString();


                            bodies.Add(body);
                        }



                        LH.Model.ST.Rdrecord10 entity = new LH.Model.ST.Rdrecord10(rdHead, bodies);
                        cRe = GetInfo.GetST10ByMOInfo(entity, CanShu.conStr);
                        if (cRe != "ok")
                        {
                            throw new Exception(cRe);
                        }
                        //新增
                        string cerr, cid, ccode;
                        if (!Rdrecord10DALCO.AddRdrecord10(entity, out cerr, out cid, out ccode))
                        {
                            throw new Exception(cerr);
                        }

                        //MessageBox.Show(id);
                        //审核
                        //LH.U8API.Bus.Pu_ArrivalVouchDAL.ConfirmArrivalVouch(id, con, u8login, true, true);
                        //MessageBox.Show("审核成功");
                         
                    //更改条码的状态
                    BarcodeManager barcodeManager = new BarcodeManager();
                    barcodeManager.ChangeBarcodestate(cParentid, "在库");
                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    //如果有错误，删除之前生成的信息
                    DelVouch(conStr,cParentid);
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }

        /// <summary>
        /// 获取收发类别
        /// </summary>
        /// <param name="conStr">数据库连接字符串</param>
        /// <param name="cType">业务类型</param>
        /// <returns></returns>
        public static string GetRdcode(string conStr,string cType)
        {
            

            string sql = string.Format(@"SELECT cValue FROM AA_Para 
WHERE  cType ='{0}'", cType);
            string cRdcode = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, conStr));
            return cRdcode;
        }

        /// <summary>
        /// 获取单据号
        /// </summary>
        /// <param name="cTable">查询的表明，WMS_TASK,WMS_RDRECORD</param>
        /// <returns></returns>
        public static string GetNewcode(string conStr,string cTable)
        {
            string cQianZhui = DateTime.Now.ToString("yyMM");
            int iMax = 0;

            string sql = string.Format(@"select MAX(CCODE) FROM  {1} where ccode like '{0}%'", cQianZhui, cTable);
            iMax = DbHelper.GetDbInt(DbHelper2.ExecuteScalar(sql, conStr));
            string cVouchCode = cQianZhui + "0001";
            if (iMax != 0)
            {
                cVouchCode = (iMax + 1).ToString();
            }
            return cVouchCode;
        }

        //删除已经生成的单据
        public static void DelVouch(string conStr,string parentid)
        {
           
            
            //删除U8的
           string  sql = string.Format(@"
delete FROM dbo.WMS_RDRECORDS_ORI WHERE PARENTID = '{0}' ", parentid);
            DbHelper2.ExecuteNonQuery(sql, conStr);
        }

       /// <summary>
       /// 根据扫描明细获取条码内容
       /// </summary>
       /// <param name="cParentid"></param>
       /// <returns></returns>
        public static DataTable GetBarcodeDetail(string cParentid)
        {
            string sql = String.Format(@"SELECT c.INVCODE,b.BATCH,SUM(a.QTY) AS QTY,a.SOURCEROWID,inv.UNITNAME,b.MAKEDATE FROM WMS_RDRECORDS_ORI  a
  INNER JOIN dbo.WMS_BARCODES b ON a.BARCODENo = b.BARCODENO
  INNER JOIN  dbo.WMS_BARCODE c ON b.BARCODENO = c.BARCODENO
  LEFT JOIN dbo.AA_INVENTORY inv ON c.INVCODE = inv.INVCODE
WHERE A.PARENTID ='{0}'
  GROUP BY c.INVCODE,b.BATCH,SOURCEROWID,inv.UNITNAME,b.MAKEDATE ", cParentid);
            return DbHelper2.ExecuteTable(sql, conStr);
        }


        /// <summary>
        /// OMS,欧瑞京根据扫描明细获取条码内容
        /// </summary>
        /// <param name="cParentid"></param>
        /// <returns></returns>
        public static DataTable OMS_GetBarcodeDetail(string conStr,string cParentid)
        {
            string sql = String.Format(@"SELECT SOURCEROWID,INVCODE,BATCH,POSCODE,POSCODE2, SUM(QTY) AS QTY 
FROM dbo.WMS_RDRECORDS_ORI WHERE PARENTID ='{0}' GROUP BY INVCODE,BATCH,POSCODE,POSCODE2,SOURCEROWID
  ", cParentid);
            return DbHelper2.ExecuteTable(sql, conStr);
        }

      

    }
}
